#导入模块
import pygame
from pygame.locals import *
import time,random

class heroplane:
    '''定义玩家飞机'''
    def __init__ (self,screen_temp):
        self.x = 200
        self.y = 400
        self.screen = screen_temp
        self.image = pygame.image.load('.\飞机大战素材图片\images\me.png')
        self.bullet_list = []  #玩家的子弹列表
    def display(self):
        '''绘制玩家飞机'''
        self.screen.blit(self.image,(self.x,self.y))
        '''绘制子弹'''
        for b in self.bullet_list:
            b.display()
            if b.move():
                self.bullet_list.remove(b)
    def move_left(self):
        '''左移动飞机'''
        self.x -= 10
        if self.x <= 0:
            self.x = 0
    def move_right(self):
        '''右移动飞机'''
        self.x += 10
        if self.x >= 419:
            self.x = 419
    def move_up(self):
        '''上移动飞机'''
        self.y -= 5
        if self.y <= 0:
            self.y = 0
    def move_down(self):
        '''下移动飞机'''
        self.y += 5
        if self.y >= 475:
            self.y = 475
    def fire(self):
        if random.choice(range(6)) in (2,4):
            self.bullet_list.append(bullet(self.screen,self.x,self.y))

    def suvive(self,e):
        if e.x>self.x+5 and e.x<self.x+93 and e.y>self.y+5 and e.y<self.y+70:
                return True
class bullet:
    '''定义子弹'''
    def __init__ (self,screen_temp,x,y):
        self.x = x + 50
        self.y = y
        self.screen = screen_temp
        self.image = pygame.image.load('.\飞机大战素材图片\images\pd.png')
    def display(self):
        '''绘制子弹'''
        self.screen.blit(self.image,(self.x,self.y))
    def move(self):
        self.y -= 10
        if self.y <= -20:
            return True


class EnemyPlane:
    '''定义敌机'''
    def __init__ (self,screen_temp):
        self.x = random.choice(range(0,408))
        self.y = -75
        self.screen = screen_temp
        self.image = pygame.image.load('.\飞机大战素材图片\images\e2.png')
        self.enemybullet_list = []
        self.times = 0
    def display(self):
        '''绘制敌机'''
        self.screen.blit(self.image,(self.x,self.y))
        for b in self.enemybullet_list:
            b.display()
            b.move()
    def move(self,hero):
        self.y += 5
        if self.y>568:
            return True
        for b in hero.bullet_list:
            if b.x>self.x+5 and b.x<self.x+93 and b.y>self.y+15 and b.y<self.y+65:
                hero.bullet_list.remove(b)
                return True
    def fire(self):
        if random.choice(range(200))==10:
            self.enemybullet_list.append(enemybullet(self.screen,self.x,self.y))
            print("enemy fire")

class enemybullet:
    '''定义敌机子弹'''
    def __init__ (self,screen_temp,x,y):
        self.x = x + 45
        self.y = y+74
        self.screen = screen_temp
        self.image = pygame.image.load('.\飞机大战素材图片\images\enemybullet.png')
    def display(self):
        '''绘制敌机子弹'''
        self.screen.blit(self.image,(self.x,self.y))
    def move(self):
        self.y += 7
        if self.y >= 1000:
            return True
def key_control(hero_temp):

    #执行退出操作
    for event in pygame.event.get():
        if event.type == QUIT:
            print('exit()')
            exit()
    #获取按键信息
    pressed_keys = pygame.key.get_pressed()
    #做判断，并执行对象的操作
    if pressed_keys[K_LEFT] or pressed_keys[K_a]:
        print('Left,,,')
        hero_temp.move_left()
    elif pressed_keys[K_RIGHT] or pressed_keys[K_d]:
        print('Right,,,')
        hero_temp.move_right()
    elif pressed_keys[K_UP] or pressed_keys[K_w]:
        print('Up,,,')
        hero_temp.move_up()
    elif pressed_keys[K_DOWN] or pressed_keys[K_s]:
        print('Down,,,')
        hero_temp.move_down()
    if pressed_keys[K_SPACE]:
        print('space...')
        hero_temp.fire()
def main():
    '''主程序函数'''
    #创建游戏窗口
    screen = pygame.display.set_mode((512,568),0,0)
    expolde1 = pygame.image.load(".\飞机大战素材图片\images\\boom1.png")
    expolde2 = pygame.image.load(".\飞机大战素材图片\images\\boom2.png")
    expolde3 = pygame.image.load(".\飞机大战素材图片\images\\boom3.png")
    expolde4 = pygame.image.load(".\飞机大战素材图片\images\\boom4.png")

    #创建游戏背景
    background = pygame.image.load('.\飞机大战素材图片\images\\bg2.jpg')

    # 创建玩家飞机
    hero = heroplane(screen)
    m = -968
    enemylist = [] # 存放敌机的列表
    die_plane = []
    live = 0
    times = 0

    while True:
        #绘制画面
        screen.blit(background,(0,m))
        m += 2
        if m >= -200:
            m = -968
        if live==0:
            hero.display()
        elif live == 1:
            if times == 0:
                    screen.blit(expolde1,(hero.x,hero.y))
            elif times == 2:
                    screen.blit(expolde2,(hero.x,hero.y))
            elif times == 4:
                    screen.blit(expolde3,(hero.x,hero.y))
            elif times == 6:
                    screen.blit(expolde4,(hero.x,hero.y))
                    times = 0
                    live = 2
            times += 1
        #执行键盘控制
        key_control(hero)
        # 随机绘制敌机
        if random.choice(range(50)) == 10:
            enemylist.append(EnemyPlane(screen))

            # 遍历敌机位置及运动
        for em in enemylist:
            em.display()
            em.fire()
            if em.move(hero):
                enemylist.remove(em)
                die_plane.append(em)
            for b in em.enemybullet_list:
                if hero.suvive(b):

                    if live!=2:
                        live = 1
                    em.enemybullet_list.remove(b)
            #爆炸画面
            for p in die_plane:
                p.times += 1
                if p.times == 1:
                    screen.blit(expolde1,(p.x,p.y))
                elif p.times == 3:
                        screen.blit(expolde2,(p.x,p.y))
                elif p.times == 5:
                        screen.blit(expolde3,(p.x,p.y))
                elif p.times == 7:      
                        screen.blit(expolde4,(p.x,p.y))
                        die_plane.remove(p)
        #更新显示
        pygame.display.update()

        #定时显示
        time.sleep(0.04)
#判断当前是否是主运行程序，并调用

if __name__ == '__main__':
    main()